package com.epam.text_elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.epam.matchers.WordMatcher;
import com.epam.tasks.WordNumComparator;

public class Text extends ArrayList<Paragraph> {

	private static final long serialVersionUID = 11265L;

	public Text(List<String> lines) {
		for (String line: lines) {
			this.add(new Paragraph(line));
		}
	}
	
	public String toString() {
		String res="";
		int numOfParagraphs=this.size();
		//all the paragraphs but the last one ("\n" at the end)
		for (int i=0; i<numOfParagraphs-1; i++) {
			res+=this.get(i).toString()+"\n";
		}
		//last paragraph (no "\n")
		res+=this.get(numOfParagraphs-1).toString();
		return res;
	}
	
	public List<Word> getAllWords() {
		List<Word> res = new ArrayList<Word>();
		WordMatcher wordMatcher = new WordMatcher(this.toString());
		Word word;
		while ((word = wordMatcher.getNextWord()) != null) {
			res.add(word);
		}
		return res;
	}
	
	public List<Sentence> getAllSentences() {
		List<Sentence> res = new ArrayList<Sentence>();
		List<Sentence> temp = new ArrayList<Sentence>();
		for (Paragraph paragraph: this) {
			temp=paragraph.getAllSentences();
			res.addAll(temp);
		}
		return res;
	}
	
	public String getSentencesSortedByNumWords() {
		String res="TASK NUMBER 2\n";
		List<Sentence> listOfSentences = getAllSentences();
		Collections.sort(listOfSentences, new WordNumComparator());
		for (Sentence sentence:listOfSentences) {
			if (sentence.getNumOfWords()!=0) {
				res+=sentence.toString()+"\n";
			}
		}
		return res;
	}
}
